import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author xhw
 */

public class DataTableReader {
    //读取CSV文件的类，传入一个inputStream，返回一个DataTable对象
    public DataTable read(InputStream inputStream) {
        //创建一个DataTable对象
        DataTable dataTable = new DataTable();
        //第一行是表头，后面的行是数据
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            //读取表头
            String line;
            if ((line = reader.readLine()) != null) {
                String[] columns = line.split(",");
                dataTable.addColumns(columns);
            }
            //读取数据
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(",");
                dataTable.addRow(values);
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return dataTable;
    }
}
